<?php

class Menu
{
    var $pdo = null;
    var $logger = null;

    function __construct()
    {
        $this->pdo = PDOWrapper::instance();
        $this->logger = CLog::getInstance();
    }


    /**
     * @return array|bool
     * 
     * 菜单
     */
    function MenuV1($GetParams, $PostArray){
        
        $user_info = User::GetInfoByToken($GetParams['token']);
        if($user_info === false) {
            return false;
        }
        
        $menu_info = self::menu_detail($user_info);
        if ($menu_info === false) {
            return false;
        }
        $message = [
            'menu' => $menu_info
        ];
        Utils::ShowMessage($message);
        return true;
    }

    function RecipeDetailV1($GetParams, $PostArray){

        $user_info = User::GetInfoByToken($GetParams['token']);
        if($user_info === false) {
            return false;
        }
        if (empty($GetParams['recipe_id'])) {
            Utils::ShowError(ErrorDefine::SHORT_OF_RECIPEID);
            return false;
        }

        $sql = sprintf("SELECT recipe.recipe_id,recipe.description,recipe.type_id,recipe_type.type_name,recipe.r_id,recipe.`name`,recipe.pic_url,recipe.price,recipe_detail.d_num,recipe_detail.spicy,recipe_detail.recommend,CASE recipe_feature.name WHEN ' ' THEN ' ' ELSE recipe_feature.name END AS feature_name FROM `recipe` LEFT JOIN recipe_detail ON recipe_detail.recipe_id=recipe.recipe_id LEFT JOIN recipe_type ON recipe_type.type_id=recipe.type_id LEFT JOIN recipe_feature ON recipe_detail.feature_id=recipe_feature.feature_id WHERE recipe.`status`=1 AND recipe.recipe_id=%s",$GetParams['recipe_id'] );
        $recipe_detail = $this->pdo->query($sql);

        // $message = [
        //     'recipe_detail' => $recipe_detail
        // ];

        Utils::ShowMessage($recipe_detail[0]);
        return true;
    }

    function MenuSearchV1($GetParams, $PostArray){

        $user_info = User::GetInfoByToken($GetParams['token']);
        if($user_info === false) {
            return false;
        }
        if (empty($GetParams['name'])) {
            Utils::ShowError(ErrorDefine::SHORT_OF_RECIPE_NAME);
            return false;
        }

        $menu_info = self::menu_detail($user_info,$GetParams['name']);
        if ($menu_info === false) {
            $menu_info = [];
        }

        $message = [
            'menu' => $menu_info
        ];
        Utils::ShowMessage($message);
        return true;
    }

    function menu_detail($user_info,$name=null){

        $table_id = $user_info['table_id'];
        $r_id = $user_info['r_id'];
        $params = [
            'r_id' => $r_id
        ];
        $check_restaurant = $this->pdo->select('restaurant',array('r_name'),$params);
        if (empty($check_restaurant)) {
            Utils::ShowError(ErrorDefine::ILLEGALL_RESTAURANT);
            return false;
        }

        $menu_info = $this->pdo->select('recipe_type',array('type_id','type_name'),$params);
        if (empty($menu_info)) {
            Utils::ShowError(ErrorDefine::SELECT_TABLE_FAILED);
            return false;
        }

        $sql = sprintf("SELECT recipe.recipe_id,recipe.description,recipe.type_id,recipe_type.type_name,recipe.r_id,recipe.`name`,recipe.pic_url,recipe.price,recipe_detail.d_num,recipe_detail.spicy,recipe_detail.recommend,CASE recipe_feature.name WHEN ' ' THEN ' ' ELSE recipe_feature.name END AS feature_name FROM `recipe` LEFT JOIN recipe_detail ON recipe_detail.recipe_id=recipe.recipe_id LEFT JOIN recipe_type ON recipe_type.type_id=recipe.type_id LEFT JOIN recipe_feature ON recipe_detail.feature_id=recipe_feature.feature_id WHERE recipe.`status`=1 AND recipe.r_id=%s",$r_id );

        if ($name != null) {
            $sql .= " AND recipe.`name` LIKE '%".$name."%'";
        }
        
        $reci_info = $this->pdo->query($sql);
        
        if (empty($reci_info)) {
            return false;
        }

        $table = new Table();
        $order_info = $table->get_order($r_id,$table_id);

        if ($order_info === false) {
            return false;
        }
        
        foreach ($reci_info as $key => $value) {
            $reci_info[$key]['order_num'] = 0;
            foreach ($order_info as $k => $v) {
                if ($value['recipe_id'] == $v['recipe_id']) {
                    $reci_info[$key]['order_num'] = $v['order_num'];
                }
            }
            
        }

        foreach ($menu_info as $key => $value) {
            $menu_info[$key]['recipe'] = [];
            foreach ($reci_info as $k => $v) {
                if ($value['type_id'] == $v['type_id']) {
                    array_push($menu_info[$key]['recipe'], $v);
                    unset($reci_info[$k]);
                }
            }
        }

        return $menu_info;
    }
}